<form class="layui-form layui-box" style='padding:25px 30px 20px 0' action='{:url()}' method="post" id="myform">

    <div class="layui-form-item">
        <label class="layui-form-label">明星图片</label>
        <div class="layui-input-block">
        	<img data-tips-image="" style="height:auto;max-height:100px;min-width:100px" src="" id="img_src"/>
            <input type="hidden" id="img" name="img" value="" required="required" lay-verify="required" class="layui-input">
            <input type="file" name="file" class="layui-upload-file" id="img_file" >
        </div>
    </div>

    <div class="hr-line-dashed"></div>

    <div class="layui-form-item text-center">
        <input type='hidden' value='${id}' name='id'/>
        <button class="layui-btn" type='button' lay-submit lay-filter="*">保存数据</button>
        <button class="layui-btn layui-btn-danger" type='button' data-confirm="确定要取消编辑吗？" data-close>取消</button>
    </div>
</form>
<div class="loading_img"></div>
<script>
layui.use(['form','upload'], function(){
	var form = layui.form();
	
	//监听提交
    form.on('submit(*)', function (data) {
    	console.log(data.field);
    	$.post('star/saveImg', data.field, function (result) { 
    		if(result.code==1){
	        	layer.closeAll();
	            layer.msg(result.msg, {
	                icon: 1,
	                shade: this.shade,
	                scrollbar: false,
	                time: 2000,
	                shadeClose: true
	            });//保存成功提示
	            $.form.reload();//刷新表单
	        }else{
	            layer.msg(result.msg, {
	                icon: 2,
	                shade: this.shade,
	                scrollbar: false,
	                time: 3000,
	                shadeClose: true
	            });
	        }
	        
		},"json");
        return false;
    });
	
	  layui.upload({
		  elem: '#img_file',
		  url: 'file/upload',
		  ext: 'jpg|png|gif|jpeg',
		  before:function(res){
			  $(".loading_img").show();
		  },
		  error:function(res){
			  layer.msg("上传失败", {
	                icon: 2,
	                shade: this.shade,
	                scrollbar: false,
	                time: 3000,
	                shadeClose: true
	           });
			  $(".loading_img").hide();
		  },
		  success: function(res){
		    console.log(res); //上传成功返回值，必须为json格式
		    if(res.code==0){
		    	var img_str = res.data.path;
		    	$("#img_src").attr("src",img_str);
		    	$("#img").val(img_str);
		    }else{
		    	layer.msg("上传失败", {
	                icon: 2,
	                shade: this.shade,
	                scrollbar: false,
	                time: 3000,
	                shadeClose: true
	            });
		    }
		    $(".loading_img").hide();
		  }
		});
	  
});

</script>